using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;

namespace Super
{
    public class FWRefManager : MonoBehaviour
    {
        public Camera mainCamera;
        public Camera UICamera;
        public Transform virtualCameraTF;
        public Transform UIRootTF;
        public Transform worldUIRootTF;
        public Transform ManagerBoxTf;
        public EventSystem eventSystem;
        public AudioListener audioListener;

        private static FWRefManager ins;
        public static FWRefManager Ins
        {
            get
            {
                if (ins == null)
                {
                    ins = FindObjectOfType<FWRefManager>();
                }

                if (ins == null)
                {
                    GameObject go = GameObject.Instantiate(AssetSettingSO.Ins.SuperFramework);
                    go.name = "SuperFramework";
                    ins = go.GetComponent<FWRefManager>();
                }
                return ins;
            }
        }

        //非play模式下，awake不会调用
        private void Awake()
        {
            if (ins != null && ins != this)
            {
                Destroy(this);
                return;
            }

            ins = this;
            OnInit();
        }

        public void OnInit()
        {
            CheckMainCamera();
            CheckAudioListens();
            CheckEventSystems();
            SceneManager.sceneLoaded += OnSceneLoad;
            SceneManager.sceneUnloaded += OnSceneUnload;
        }

        #region  回调函数

        public void OnSceneLoad(Scene scene, LoadSceneMode mode)
        {
            CheckMainCamera();
            CheckAudioListens();
            CheckEventSystems();
        }

        public void OnSceneUnload(Scene scene)
        {
            mainCamera.gameObject.SetActive(false);
            audioListener.enabled = false;
            eventSystem.gameObject.SetActive(false);
        }

        #endregion

        #region  检查函数
        public void CheckMainCamera()
        {
            Camera sceneMainCamera = null;
            bool hasOther = false;
            GameObject[] objs = GameObject.FindGameObjectsWithTag("MainCamera");
            for (int i = 0; i < objs.Length; i++)
            {
                Camera camera = objs[i].GetComponent<Camera>();
                bool isValid = camera != null && camera != this.mainCamera && objs[i].gameObject.activeSelf;
                if (!isValid) continue;

                UniversalAdditionalCameraData cameraURPData = camera.GetUniversalAdditionalCameraData();
                if (cameraURPData.renderType != CameraRenderType.Base) continue;

                sceneMainCamera = camera;
                hasOther = true;
                break;
            }

            mainCamera.gameObject.SetActive(!hasOther);
            if (hasOther)
            {
                FWGameUtil.MainCameraStackAdd(sceneMainCamera, UICamera);
            }
        }

        public void CheckAudioListens()
        {
            AudioListener[] audios = GameObject.FindObjectsOfType<AudioListener>();
            bool hasOther = false;
            for (int i = 0; i < audios.Length; i++)
            {
                if (audios[i] != audioListener && audios[i].gameObject.activeSelf)
                {
                    hasOther = true;
                    break;
                }
            }

            audioListener.enabled = !hasOther;
        }

        public void CheckEventSystems()
        {
            EventSystem[] events = GameObject.FindObjectsOfType<EventSystem>();
            bool hasOther = false;
            for (int i = 0; i < events.Length; i++)
            {
                if (events[i] != eventSystem && events[i].gameObject.activeSelf)
                {
                    hasOther = true;
                    break;
                }
            }

            eventSystem.gameObject.SetActive(!hasOther);
        }

        #endregion
    }

}
